package ManHinhChinh;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import ManHinhChinh.ConnectSQL;;
public class HoaDonBanLe {

	protected Shell shlHanBn;
	private Table table;
	private Text txt_HDBL_DienThoai;
	private Text txt_HDBL_DiaChi;
	private Text txt_HDBL_KhachHang;
	private Text txt_HDBL_MaHoaDon;
	private Text txtSLLHH;
	private String slTHH = "SELECT * FROM HANG_HOA";
	private TableItem[] items;
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shlHanBn.open();
		shlHanBn.layout();
		while (!shlHanBn.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		shlHanBn = new Shell();
		shlHanBn.setSize(658, 558);
		shlHanBn.setText("H\u00F3a \u0110\u01A1n B\u00E1n L\u1EBB");
		
		table = new Table(shlHanBn, SWT.BORDER | SWT.MULTI);
		table.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
		table.setBounds(10, 271, 622, 173);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(43);
		tblclmnNewColumn.setText("STT");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(268);
		tblclmnNewColumn_1.setText("T\u00EAn H\u00E0ng");
		
		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(65);
		tblclmnNewColumn_3.setText("S\u1ED1 L\u01B0\u1EE3ng");
		
		TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_4.setWidth(99);
		tblclmnNewColumn_4.setText("\u0110\u01A1n Gi\u00E1");
		
		TableColumn tblclmnNewColumn_5 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_5.setWidth(142);
		tblclmnNewColumn_5.setText("Th\u00E0nh Ti\u1EC1n");
		

		 final DateTime DateTime_NgayBan = new DateTime(shlHanBn, SWT.BORDER);
			DateTime_NgayBan.setBounds(167, 184, 112, 24);
			
		Button btnNewButton = new Button(shlHanBn, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String a = txt_HDBL_MaHoaDon.getText().trim();
				String b = DateTime_NgayBan.toString().substring(10, DateTime_NgayBan.toString().length()-1);
				String c = txt_HDBL_KhachHang.getText().trim();
				String d = txt_HDBL_DiaChi.getText().trim();	
					if (c.equals(""))
					{
						MessageBox cc = new MessageBox(shlHanBn, SWT.ICON_ERROR | SWT.OK);
						cc.setText("ERROR");
						cc.setMessage("Bạn chưa nhập tên khách hàng...");
						cc.open();
					} else if (d.equals(""))
							{

							MessageBox dd = new MessageBox(shlHanBn, SWT.ICON_ERROR | SWT.OK);
							dd.setText("ERROR");
							dd.setMessage("Bạn chưa nhập địa chỉ khách hàng...");
							dd.open();
							}
					else {
						MessageBox mss = new MessageBox(shlHanBn, SWT.ICON_INFORMATION | SWT.OK);
						mss.setText("Thành Công");
						mss.setMessage("Thêm Hóa Đơn Thành Công...");
						mss.open();
					}
				ConnectSQL cn = new ConnectSQL();
				cn.GhiHoaDonBanLe(a,b,c,d);
				
				
			}
		});
		btnNewButton.setImage(SWTResourceManager.getImage("D:\\Java_WorkSpace\\_QuanLyBanHang\\image\\Save.ico"));
		btnNewButton.setBounds(267, 472, 79, 37);
		btnNewButton.setText("L\u01B0u");
		
		txt_HDBL_MaHoaDon = new Text(shlHanBn, SWT.BORDER);
		txt_HDBL_MaHoaDon.setEnabled(false);
		txt_HDBL_MaHoaDon.setBounds(167, 10, 161, 21);
		DataUser dt = new DataUser();
		txt_HDBL_MaHoaDon.setText(dt.taoMaHDBL());
		txt_HDBL_KhachHang = new Text(shlHanBn, SWT.BORDER);
		txt_HDBL_KhachHang.setBounds(167, 53, 161, 21);
		
		txt_HDBL_DiaChi = new Text(shlHanBn, SWT.BORDER);
		txt_HDBL_DiaChi.setBounds(167, 101, 161, 21);
		
		txt_HDBL_DienThoai = new Text(shlHanBn, SWT.BORDER);
		txt_HDBL_DienThoai.setBounds(167, 139, 161, 21);
		txt_HDBL_DienThoai.addListener(SWT.Verify, new Listener() {
		      public void handleEvent(Event e) {
		        String string = e.text;
		        char[] chars = new char[string.length()];
		        string.getChars(0, chars.length, chars, 0);
		        for (int i = 0; i < chars.length; i++) {
		          if (!('0' <= chars[i] && chars[i] <= '9')) {
		            e.doit = false;
		            return;
		          }
		        }
		      }
		    });
		
		
		Label lblNewLabel_1 = new Label(shlHanBn, SWT.NONE);
		lblNewLabel_1.setBounds(37, 10, 87, 24);
		lblNewLabel_1.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		lblNewLabel_1.setText("M\u00E3 H\u00F3a \u0110\u01A1n");
		
		Label lblNewLabel = new Label(shlHanBn, SWT.NONE);
		lblNewLabel.setBounds(37, 53, 88, 24);
		lblNewLabel.setFont(SWTResourceManager.getFont("Times New Roman", 13, SWT.NORMAL));
		lblNewLabel.setText("Kh\u00E1ch H\u00E0ng");
		
		Label lblaCh = new Label(shlHanBn, SWT.NONE);
		lblaCh.setBounds(37, 101, 67, 21);
		lblaCh.setFont(SWTResourceManager.getFont("Times New Roman", 13, SWT.NORMAL));
		lblaCh.setText("\u0110\u1ECBa Ch\u1EC9");
		
		Label lblinThoi = new Label(shlHanBn, SWT.NONE);
		lblinThoi.setBounds(37, 139, 88, 24);
		lblinThoi.setFont(SWTResourceManager.getFont("Times New Roman", 13, SWT.NORMAL));
		lblinThoi.setText("\u0110i\u1EC7n Tho\u1EA1i");
		
		
		                  
		
		
		
		Label lblNgyBn = new Label(shlHanBn, SWT.NONE);
		lblNgyBn.setBounds(37, 184, 88, 24);
		lblNgyBn.setFont(SWTResourceManager.getFont("Times New Roman", 13, SWT.NORMAL));
		lblNgyBn.setText("Ng\u00E0y B\u00E1n");
		
		Label lblSLngLoi = new Label(shlHanBn, SWT.NONE);
		lblSLngLoi.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		lblSLngLoi.setBounds(37, 233, 225, 24);
		lblSLngLoi.setText("Nh\u1EADp Chi Ti\u1EBFt S\u1ED1 L\u01B0\u1EE3ng H\u00E0ng H\u00F3a");
		
		txtSLLHH = new Text(shlHanBn, SWT.BORDER);
		txtSLLHH.setText("1");
		txtSLLHH.setBounds(284, 233, 76, 21);
		// CHá»‰ cho nháº­p chá»¯ sá»‘ vÃ o texbox
		txtSLLHH.addListener(SWT.Verify, new Listener() {
		      public void handleEvent(Event e) {
		        String string = e.text;
		        char[] chars = new char[string.length()];
		        string.getChars(0, chars.length, chars, 0);
		        for (int i = 0; i < chars.length; i++) {
		          if (!('0' <= chars[i] && chars[i] <= '9')) {
		            e.doit = false;
		            return;
		          }
		        }
		      }
		    });
		
		final Button btnXacNhan = new Button(shlHanBn, SWT.NONE);
		btnXacNhan.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btnXacNhan.setEnabled(false);
				
				for(int i =1;i<=Integer.parseInt(txtSLLHH.getText());i++)
				{
					TableItem item = new TableItem(table, SWT.NONE);
					item.setText(0,""+i+"");
				}
				items = table.getItems();
				
				 
				
			    for (int i = 0; i < items.length; i++) {
			      TableEditor editor = new TableEditor(table);
			      CCombo combo = new CCombo(table, SWT.NONE);
			      combo.setText("Lựa Chọn mặt hàng");
			     

			      TaoKetNoi s = new TaoKetNoi();
					Connection con = s.TaoLienKet();
					
					Statement stm = null;
					ResultSet rs = null;
					try {
						stm = con.createStatement();
						rs = stm.executeQuery(slTHH);
						while (rs.next())
						{
							String key1 = rs.getString("TENHANGHOA");
							combo.add(key1);
							combo.setData(key1, rs.getString("MAHANGHOA"));
							
						}
						con.close();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
			      
			      editor.grabHorizontal = true;
			      editor.setEditor(combo, items[i], 1);	  
			      
			      editor = new TableEditor(table);
			      Text text1 = new Text(table, SWT.NONE);  
			      editor.grabHorizontal = true;
			      editor.setEditor(text1, items[i], 2);
			      
			      editor = new TableEditor(table);
			      Text text2 = new Text(table, SWT.NONE);
			      editor.grabHorizontal = true;
			      editor.setEditor(text2, items[i], 3);
			      
			}
			    
			    
			    
			}
		});
		btnXacNhan.setBounds(398, 233, 75, 25);
		btnXacNhan.setText("X\u00E1c Nh\u1EADn");
		
		Button btnNewButton_1 = new Button(shlHanBn, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				 for (int i = 0; i < items.length; i++) {
					 TableEditor editor = new TableEditor(table);
				 }
				
			}
		});
		btnNewButton_1.setBounds(489, 445, 143, 25);
		btnNewButton_1.setText("Thành Tiền");

	}
}
